package com.vieewer.btdemo.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import com.vieewer.btdemo.R;

public class MyProgressBar extends View {

    private Paint mPaint = null ;
    private int max = 100;
    private int progress = 0;
    private int type = 1;
    private int NORMAL_TYPE = 1;
    private int ALERT_TYPE = 2;
    private Context mContext;

    public MyProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyProgressBar); //与属性名称一致
        max = array.getInteger(R.styleable.MyProgressBar_max, 100);//第一个是传递参数，第二个是默认值
        progress = array.getInteger(R.styleable.MyProgressBar_progress, 0);
        type = array.getInt(R.styleable.MyProgressBar_type, 1);
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Point left_top=new Point(0,0);
        Point right_bottom=new Point(getWidth(),getHeight());
        double rate=(double)progress/(double)max;
        drawProgressBar(canvas, left_top, right_bottom, rate);
    }
    public void setProgress(int progress){
        this.progress=progress;
        invalidate();//使得onDraw重绘
    }
    private void drawProgressBar(Canvas canvas,Point left_top,Point right_bottom,double rate){
        int width=1;
        int rad=5;
        mPaint.setColor(ContextCompat.getColor(mContext, R.color.gray));//画边框
        mPaint.setStyle(Paint.Style.FILL);
        //mPaint.setStrokeWidth(width);
        RectF rectF = new RectF(left_top.x, left_top.y, right_bottom.x, right_bottom.y);
        canvas.drawRoundRect(rectF, rad, rad, mPaint);
 
        mPaint.setColor(ContextCompat.getColor(mContext, R.color.white));//画progress bar
     /*   if (type==ALERT_TYPE) {
            if (rate>0.9)
            mPaint.setColor(Color.RED);
        }*/

        mPaint.setStyle(Paint.Style.FILL);
        int x_end=(int)(right_bottom.x*rate);
        RectF rectF2 = new RectF(left_top.x+width,left_top.y+width,x_end-width,right_bottom.y-width);
        canvas.drawRoundRect(rectF2, rad, rad, mPaint);
    }
}